package cn.kgc.ssm.Interceptor;

import cn.kgc.ssm.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("预处理方法，在controller前执行");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(null != user){
            return true;
        }else {
            request.setAttribute("msg","您需要先登录后访问");
            request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request,response);
            return false;
        }
    }

//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("后处理方法，在controller后执行");
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        System.out.println("最终方法，在jsp页面打开后执行");
//    }
}
